package Review;

public class Sort {
    public static void selectSort(int[] array){
        // write code  here
        for (int i=0;i<array.length;i++){
            int minIndex = i;
            for (int j=i+1;j<array.length;j++){
                if (array[j]<array[minIndex]){
                    minIndex =j;
                }
            }
            swap(array,i,minIndex);
        }
    }


// 堆排序
public static void heapSort(int[] array){
        // write code  here
    creatHeap(array);
    int end =array.length-1;
    while(end>0){
        swap(array,0,end);
        siftDown(array,0,end);
        end--;
    }
    }
    private static void creatHeap(int[] array){
        for (int parent = (array.length-1-1)/2;parent>=0;parent--){
            siftDown(array,parent,array.length);
        }
    }

    private static void siftDown(int[] array, int parent, int length) {
        int child = 2*parent+1;
        while (child<array.length){
            if (child+1<array.length&&array[child]<array[child+1]){
                child++;
            }
            if (array[child]>array[parent]){
                swap(array,parent,child);
                parent =child;
                child = 2*parent+1;
            }
        }
    }

    public static void swap(int[] arr,int i,int j){
        int tmp = arr[i];
        arr[i] =arr[j];
        arr[j] = tmp;
    }
    // 冒泡排序
public static void bubbleSort(int[] array){
        // write code  here
    for (int i =0;i<array.length-1;i++){
        boolean flg =false;
        for (int j=i+1;j<array.length-1-i;j++){
            if (array[j+1]<array[j]){
                swap(array,j,j+1);
                flg =true;
            }
        }
        if (!flg){
            break;
        }
    }
    }

// 快速
public static void quickSort(int[] array){
        // write code  here
    quick(array,0,array.length-1);

    }
    private  static void  quick(int[] array,int start,int end){
        if(start>=end){
            return;
        }
        int minIndex = gerMiddleNum(array,start,end);
        swap(array,start,minIndex);
        int pivot = partiton(array,start,end);
        quick(array,start,pivot-1);
        quick(array,pivot+1,end);
    }

    private static int partiton(int[] array, int left, int right) {
        int tmp = array[left];
        while (left<right){
            while (left<right&&array[right]>=tmp){
                right--;
            }
            array[left] = array[right];
            while (left<right&&array[left]<=tmp){
                left++;
            }
           array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    private static int gerMiddleNum(int[] array, int left, int right) {
        int mid =(left+right)/2;
        if (array[left]<array[right]) {
                if (array[left]<array[right]){
                    return left;
                } else if (array[mid]>array[right]) {
                    return right;
                }else {
                    return mid;
                }
        }else{
            if (array[mid]>array[left]){
                return left;
            } else if (array[mid]<array[right]) {
                return right;
            }else{
                return mid;
            }
        }
    }
    // 归并排序---递归
public static void mergeSort(int[] array){
        // write code  here
    mergeT(array,0,array.length-1);

    }
    private  static void mergeT(int[] array,int left,int right){
        if(left>=right){
            return;
        }
        int mid =(left+right)/2;
        mergeT(array,left,mid);
        mergeT(array,mid+1,right);
        //写到这里已经全部分解完毕了
        //开始合并
        merge(array,left,mid,right);
    }
    private static void merge(int[] array,int left,int mid,int right){
        int[] tmp = new int[right-left+1];
        int k=0;
        int s1 = left;
        int s2 =mid+1;
        while (s1<=mid&&s2<=right){
            if (array[s1]<array[right]){
                tmp[k++] = array[s1++];
            }else {
                tmp[k++] = array[s2++];
            }
        }
        while (s1<=mid){
            tmp[k++] = array[s1++];
        }
        while (s2<right){
            tmp[k++] = array[s2++];
        }
        for (int i = 0; i < k; i++) {
            array[i+left] =tmp[i];
        }
    }


// 归并排序---非递归
public static void mergeSortNor(int[] array){
        // write code  here
    int gap =1;
    while (gap<array.length){
        for (int i = 0; i < array.length; i=i+gap*2) {
          int left =i;
          int mid = left+gap-1;
          if(mid>=array.length){
              mid = array.length-1;
          }
          int right = mid+gap;
          if(right>=array.length){
              right = array.length-1;
          }
          merge(array,left,mid,right);
        }
        gap*=2;
    }
    }

// 计数排序
public static void countSort(int[] array){
        // write code  here
        int minVal = array[0];
        int maxVal = array[0];
    for (int i = 0; i < array.length; i++) {
        if(array[i]<minVal){
            minVal = array[i];
        }
        if (array[i]>maxVal){
            maxVal = array[i];
        }
    }
    int len = maxVal-minVal+1;
    int[] count = new int[len];
    for (int i = 0; i < array.length; i++) {
        int index = array[i];
        count[i-maxVal]++;
    }
    int index =0;
    for (int i = 0; i < count.length; i++) {
        while (count[i]!=0){
            array[index] = i+minVal;
            index++;
            count[i]--;
        }
    }
    }
}

